//! 路由器模块
//! 为不同的web框架提供统一的路由注册接口



/// 路由信息
#[derive(Debug, Clone)]
pub struct RouteInfo {
    pub path: String,
    pub method: String,
    pub handler_name: String,
    pub auth_policy: Option<String>,
    pub description: Option<String>,
    pub tags: Vec<String>,
}

impl RouteInfo {
    pub fn new(path: &str, method: &str, handler_name: &str) -> Self {
        Self {
            path: path.to_string(),
            method: method.to_string(),
            handler_name: handler_name.to_string(),
            auth_policy: None,
            description: None,
            tags: Vec::new(),
        }
    }

    pub fn with_auth_policy(mut self, policy: &str) -> Self {
        self.auth_policy = Some(policy.to_string());
        self
    }

    pub fn with_description(mut self, desc: &str) -> Self {
        self.description = Some(desc.to_string());
        self
    }

    pub fn with_tags(mut self, tags: Vec<&str>) -> Self {
        self.tags = tags.iter().map(|s| s.to_string()).collect();
        self
    }
}

/// 路由注册器特征
pub trait Router {
    type Response;
    type Error;

    fn register_route(&mut self, route_info: RouteInfo) -> Result<(), Self::Error>;
    fn build(self) -> Self::Response;
}

/// 路由构建器
pub struct RouteBuilder {
    routes: Vec<RouteInfo>,
}

impl RouteBuilder {
    pub fn new() -> Self {
        Self {
            routes: Vec::new(),
        }
    }

    pub fn add_route(mut self, route_info: RouteInfo) -> Self {
        self.routes.push(route_info);
        self
    }

    #[cfg(feature = "actix")]
    pub fn build_actix(self, _auth_policy: Option<Arc<dyn AuthPolicy>>) -> Result<ActixRouter, String> {
        let router = ActixRouter { routes: self.routes };
        Ok(router)
    }

    #[cfg(feature = "rocket")]
    pub fn build_rocket(self, _auth_policy: Option<Arc<dyn AuthPolicy>>) -> Result<RocketRouter, String> {
        let router = RocketRouter { routes: self.routes };
        Ok(router)
    }

    #[cfg(feature = "axum")]
    pub fn build_axum(self, _auth_policy: Option<Arc<dyn AuthPolicy>>) -> Result<AxumRouter, String> {
        let router = AxumRouter { routes: self.routes };
        Ok(router)
    }
}

impl Default for RouteBuilder {
    fn default() -> Self {
        Self::new()
    }
}

/// Actix-web 路由器
#[cfg(feature = "actix")]
pub struct ActixRouter {
    routes: Vec<RouteInfo>,
}

#[cfg(feature = "actix")]
impl ActixRouter {
    pub fn new() -> Self {
        Self {
            routes: Vec::new(),
        }
    }
}

#[cfg(feature = "actix")]
impl Router for ActixRouter {
    type Response = String; // 简化返回类型
    type Error = String;

    fn register_route(&mut self, route_info: RouteInfo) -> Result<(), Self::Error> {
        self.routes.push(route_info);
        Ok(())
    }

    fn build(self) -> Self::Response {
        format!("ActixRouter built with {} routes", self.routes.len())
    }
}

/// Rocket 路由器
#[cfg(feature = "rocket")]
pub struct RocketRouter {
    routes: Vec<RouteInfo>,
}

#[cfg(feature = "rocket")]
impl RocketRouter {
    pub fn new() -> Self {
        Self {
            routes: Vec::new(),
        }
    }
}

#[cfg(feature = "rocket")]
impl Router for RocketRouter {
    type Response = String; // 简化返回类型
    type Error = String;

    fn register_route(&mut self, route_info: RouteInfo) -> Result<(), Self::Error> {
        self.routes.push(route_info);
        Ok(())
    }

    fn build(self) -> Self::Response {
        format!("RocketRouter built with {} routes", self.routes.len())
    }
}

/// Axum 路由器
#[cfg(feature = "axum")]
pub struct AxumRouter {
    routes: Vec<RouteInfo>,
}

#[cfg(feature = "axum")]
impl AxumRouter {
    pub fn new() -> Self {
        Self {
            routes: Vec::new(),
        }
    }
}

#[cfg(feature = "axum")]
impl Router for AxumRouter {
    type Response = String; // 简化返回类型
    type Error = String;

    fn register_route(&mut self, route_info: RouteInfo) -> Result<(), Self::Error> {
        self.routes.push(route_info);
        Ok(())
    }

    fn build(self) -> Self::Response {
        format!("AxumRouter built with {} routes", self.routes.len())
    }
}